<template>
    <div>
        <table>
            <tbody>
                <tr>
                    <td>名称</td>
                    <td>
                        <input type="text" v-model="date.name">
                    </td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td>
                        <input type="password" v-model="date.password">
                    </td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" value="1" name="sex" v-model="date.sex">男
                        <input type="radio" value="1" name="sex" v-model="date.sex">女
                    </td>
                </tr>
                <tr>
                    <td>日期</td>
                    <td>
                        <input type="date" v-model="date.birthday">
                    </td>
                </tr>
                <tr>
                    <td>电话</td>
                    <td>
                        <input type="text" v-model="date.phoneNumber">
                    </td>
                </tr>
                <tr>
                    <td>年龄</td>
                    <td>
                        <input type="text" v-model="date.age">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td><input type="checkbox" >勾选为true,否则false</td>
                </tr>
                <tr>
                    <td>等级编号</td>
                    <td>
                        <select v-model="date.memberLevelId">
                            <option value="0">请选择</option>
                            <option v-for="a in dates" :value="a.memberLevelId">{{ a.levelTitle }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="upload">
                        <img :src="'https://localhost:7253/'+date.img" width="100px" height="100px">
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" value="添加" @click="add"> 
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref,onMounted } from 'vue';
import { useRouter } from 'vue-router';
const routher = useRouter();
onMounted(()=>{
    getAdapter()
})
const upload=(e:any)=>{
    var a = e.target.files[0]
    var aa = new FormData()
    aa.append("file",a)

    if(a.size  >   1024 * 1024 *2)
    {
        alert('文件只能上传小于2MB')
        return;
    }
    if(a.type != "image/jpeg"  &&   a.type!= "image/png" )
    {
        alert('文件只能png|jpg格式')
        return;
    }

    axios.post("https://localhost:7253/api/Upload/UpFile",aa).then(res=>{
        date.value.img=res.data;
        alert("上传成功")
    })
}
const getAdapter=()=>{
    axios.get("https://localhost:7253/api/MemberLevel/GetMembers").then(res=>{
        dates.value=res.data;
    })
}

const dates=ref([{
    "memberLevelId": 0,
    "levelTitle": "",
    "state": false
}])

const add=()=>{
    axios.post("https://localhost:7253/api/Member/AddMember",date.value).then(res=>{
        if(res.data>0){
            alert("添加成功")
            routher.push({path:"/Show"})
        }
        else{
            alert("添加失败")
        }
    })
}

const date=ref({
  "id": 0,
  "name": "",
  "password": "",
  "sex": 0,
  "birthday": "",
  "phoneNumber": "",
  "age": 0,
  //"isDelete": false,
  //"createDate": "",
  "state": false,
  "memberLevelId": 0,
  "img": ""
})
</script>

<style scoped>

</style>